import gettext
from flask import Flask, request, jsonify
from babel import support

en_translations = support.Translations.load('locale', 'en')
zh_translations = support.Translations.load('locale', 'zh')


app = Flask(__name__)

# 设置默认语言
default_locale = 'en'

@app.route('/content', methods=['GET'])
def get_content():
    # 获取请求中的语言参数
    lang = request.args.get('lang', default_locale)
    
 
    if lang == 'en':
        _ = en_translations.gettext
    else:
        _ = zh_translations.gettext
    # 获取翻译文本
    content_type = request.args.get('type', 'greeting')
    if content_type == 'greeting':
        text = _("Hello, World!")
    elif content_type == 'farewell':
        text = _("Goodbye!")
    else:
        return jsonify({"error": "Invalid content type"}), 400
    
    return jsonify({content_type: text})

if __name__ == '__main__':
    app.run(debug=True)
